/**
 * Tools Demo
 *
 * Demonstrates how to use the Tools system
 */

// Set test environment to skip API key validation
process.env.NODE_ENV = 'test';

import { toolRegistry } from '@/tools/tool-factory';
import { builtinTools } from '@/tools/builtin';
import { logger } from '@/utils/logger';

async function main() {
  console.log('🛠️  CAgent Tools Demo\n');

  // Register all built-in tools
  console.log('📦 Registering built-in tools...');
  toolRegistry.registerTools(builtinTools);
  console.log(`✅ Registered ${builtinTools.length} tools\n`);

  // List all tools
  console.log('📋 Available Tools:');
  const allTools = toolRegistry.getAllTools();
  allTools.forEach(tool => {
    console.log(`  - ${tool.name}: ${tool.description}`);
  });
  console.log();

  // Test calculator tool
  console.log('🧮 Testing Calculator Tool:');
  const calculator = toolRegistry.getTool('calculator');
  if (calculator) {
    const result1 = await calculator.handler({ expression: '2 + 2' });
    console.log(`  2 + 2 = ${result1.content[0].text}`);

    const result2 = await calculator.handler({ expression: '10 * 5' });
    console.log(`  10 * 5 = ${result2.content[0].text}`);

    const result3 = await calculator.handler({ expression: '100 / 4' });
    console.log(`  100 / 4 = ${result3.content[0].text}`);
  }
  console.log();

  // Test advanced calculator
  console.log('🔬 Testing Advanced Calculator Tool:');
  const advCalc = toolRegistry.getTool('advanced_calculator');
  if (advCalc) {
    const result1 = await advCalc.handler({ operation: 'sqrt', value: 16 });
    console.log(`  sqrt(16) = ${result1.content[0].text}`);

    const result2 = await advCalc.handler({ operation: 'sin', value: 0 });
    console.log(`  sin(0) = ${result2.content[0].text}`);

    const result3 = await advCalc.handler({ operation: 'abs', value: -42 });
    console.log(`  abs(-42) = ${result3.content[0].text}`);
  }
  console.log();

  // Test evaluate expression
  console.log('📊 Testing Evaluate Expression Tool:');
  const evalTool = toolRegistry.getTool('evaluate_expression');
  if (evalTool) {
    const result1 = await evalTool.handler({ expression: 'Math.PI' });
    console.log(`  Math.PI = ${result1.content[0].text}`);

    const result2 = await evalTool.handler({ expression: 'Math.max(10, 20, 30)' });
    console.log(`  Math.max(10, 20, 30) = ${result2.content[0].text}`);
  }
  console.log();

  // Create SDK MCP server
  console.log('🔧 Creating SDK MCP Server:');
  const server = toolRegistry.createServer('demo-tools', [
    'calculator',
    'advanced_calculator',
    'evaluate_expression',
  ]);
  console.log('✅ SDK MCP server created with 3 tools\n');

  // Test error handling
  console.log('⚠️  Testing Error Handling:');
  if (calculator) {
    const errorResult = await calculator.handler({ expression: 'alert("xss")' });
    console.log(`  Invalid expression: ${errorResult.content[0].text}`);
  }
  console.log();

  console.log('🎉 Demo completed successfully!');
}

main().catch(error => {
  logger.error('Demo failed', error);
  process.exit(1);
});

